package com.seecen.controller;

import com.seecen.common.BaseResponse;
import com.seecen.common.ResultUtils;
import com.seecen.exception.ErrorCode;
import com.seecen.pojo.entity.ServiceEntity;
import com.seecen.pojo.entity.Category;
import com.seecen.service.ServiceService;
import com.seecen.service.CategoryService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: 小辛同学
 * @CreateTime: 2025-10-19
 * @Description: 管理员相关接口
 * @Version: 1.0
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Resource
    private ServiceService serviceService;
    
    @Resource
    private CategoryService categoryService;

    /**
     * 添加服务
     */
    @PostMapping("/service/add")
    public BaseResponse<?> addService(@RequestBody ServiceEntity serviceEntity) {
        try {
            // 设置创建时间和更新时间
            Date now = new Date();
            serviceEntity.setCreatetime(now);
            serviceEntity.setUpdatetime(now);
            serviceEntity.setIsdeleted(0); // 设置未删除状态
            
            // 保存服务
            boolean success = serviceService.save(serviceEntity);
            if (success) {
                log.info("添加服务成功: {}", serviceEntity.getServicename());
                return ResultUtils.success(serviceEntity);
            } else {
                log.error("添加服务失败: {}", serviceEntity.getServicename());
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "添加服务失败");
            }
        } catch (Exception e) {
            log.error("添加服务异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "添加服务异常: " + e.getMessage());
        }
    }

    /**
     * 更新服务
     */
    @PutMapping("/service/update/{serviceId}")
    public BaseResponse<?> updateService(@PathVariable Long serviceId, @RequestBody ServiceEntity serviceEntity) {
        try {
            // 设置服务ID和更新时间
            serviceEntity.setServiceid(serviceId);
            serviceEntity.setUpdatetime(new Date());
            
            // 更新服务
            boolean success = serviceService.updateById(serviceEntity);
            if (success) {
                log.info("更新服务成功: ID={}, 名称={}", serviceId, serviceEntity.getServicename());
                return ResultUtils.success(serviceEntity);
            } else {
                log.error("更新服务失败: ID={}", serviceId);
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "更新服务失败");
            }
        } catch (Exception e) {
            log.error("更新服务异常: ID={}", serviceId, e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "更新服务异常: " + e.getMessage());
        }
    }

    /**
     * 删除服务
     */
    @DeleteMapping("/service/delete/{serviceId}")
    public BaseResponse<?> deleteService(@PathVariable Long serviceId) {
        try {
            // 逻辑删除服务
            boolean success = serviceService.removeById(serviceId);
            if (success) {
                log.info("删除服务成功: ID={}", serviceId);
                return ResultUtils.success("删除服务成功");
            } else {
                log.error("删除服务失败: ID={}", serviceId);
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "删除服务失败");
            }
        } catch (Exception e) {
            log.error("删除服务异常: ID={}", serviceId, e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "删除服务异常: " + e.getMessage());
        }
    }

    // ==================== 分类管理接口 ====================

    /**
     * 获取分类列表
     */
    @GetMapping("/category/list")
    public BaseResponse<?> getCategoryList() {
        try {
            return ResultUtils.success(categoryService.list());
        } catch (Exception e) {
            log.error("获取分类列表异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "获取分类列表异常: " + e.getMessage());
        }
    }

    /**
     * 添加分类
     */
    @PostMapping("/category/add")
    public BaseResponse<?> addCategory(@RequestBody Category category) {
        try {
            // 设置创建时间和更新时间
            Date now = new Date();
            category.setCreatetime(now);
            category.setUpdatetime(now);
            if (category.getCategorytype() == null) {
                category.setCategorytype(1L); // 设置默认分类类型
            }
            
            // 保存分类
            boolean success = categoryService.save(category);
            if (success) {
                log.info("添加分类成功: {}", category.getCategoryname());
                return ResultUtils.success(category);
            } else {
                log.error("添加分类失败: {}", category.getCategoryname());
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "添加分类失败");
            }
        } catch (Exception e) {
            log.error("添加分类异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "添加分类异常: " + e.getMessage());
        }
    }

    /**
     * 更新分类
     */
    @PutMapping("/category/update/{categoryId}")
    public BaseResponse<?> updateCategory(@PathVariable Long categoryId, @RequestBody Category category) {
        try {
            // 设置分类ID和更新时间
            category.setCategoryid(categoryId);
            category.setUpdatetime(new Date());
            
            // 更新分类
            boolean success = categoryService.updateById(category);
            if (success) {
                log.info("更新分类成功: ID={}, 名称={}", categoryId, category.getCategoryname());
                return ResultUtils.success(category);
            } else {
                log.error("更新分类失败: ID={}", categoryId);
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "更新分类失败");
            }
        } catch (Exception e) {
            log.error("更新分类异常: ID={}", categoryId, e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "更新分类异常: " + e.getMessage());
        }
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/category/delete/{categoryId}")
    public BaseResponse<?> deleteCategory(@PathVariable Long categoryId) {
        try {
            // 删除分类
            boolean success = categoryService.removeById(categoryId);
            if (success) {
                log.info("删除分类成功: ID={}", categoryId);
                return ResultUtils.success("删除分类成功");
            } else {
                log.error("删除分类失败: ID={}", categoryId);
                return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "删除分类失败");
            }
        } catch (Exception e) {
            log.error("删除分类异常: ID={}", categoryId, e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "删除分类异常: " + e.getMessage());
        }
    }
}
